home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / text / faqs / lisp-faq.part5 < prev    next >
Encoding:
Internet Message Format  |  1994-05-15  |  17.4 KB

  1. Subject: FAQ: Object-oriented Programming in Lisp 5/7 [Monthly posting]
  2. Newsgroups: comp.lang.lisp,comp.lang.clos,news.answers,comp.answers
  3. From: mkant+@cs.cmu.edu (Mark Kantrowitz)
  4. Date: Fri, 13 May 1994 14:52:09 GMT
  5.  
  6. Archive-name: lisp-faq/part5
  7. Last-Modified: Thu May 12 21:33:02 1994 by Mark Kantrowitz
  8. Version: 1.45
  9.  
  10. ;;; ****************************************************************
  11. ;;; Answers to Frequently Asked Questions about Lisp ***************
  12. ;;; ****************************************************************
  13. ;;; Written by Mark Kantrowitz and Barry Margolin
  14. ;;; lisp_5.faq -- 17919 bytes
  15.  
  16. This post contains Part 5 of the Lisp FAQ. It is cross-posted to the
  17. newsgroup comp.lang.clos because it contains material of interest to
  18. people concerned with CLOS, PCL and object-oriented programming in
  19. Lisp. The other parts of the Lisp FAQ are posted only to the
  20. newsgroups comp.lang.lisp and news.answers.
  21.  
  22. If you think of questions that are appropriate for this FAQ, or would
  23. like to improve an answer, please send email to us at lisp-faq@think.com.
  24.  
  25. CLOS/PCL Questions (Part 5):
  26.  
  27.   [5-0]   What is CLOS (PCL) and where can I get it?
  28.           How do you pronounce CLOS?
  29.   [5-1]   What documentation is available about object-oriented
  30.           programming in Lisp?  
  31.   [5-2]   How do I write a function that can access defstruct slots by
  32.           name?  I would like to write something like 
  33.           (STRUCTURE-SLOT <object> '<slot-name>).   
  34.   [5-3]   How can I list all the CLOS instances in a class?
  35.   [5-4]   How can I store data and CLOS instances (with possibly circular
  36.           references) on disk so that they may be retrieved at some later
  37.           time?
  38.   [5-5]   Given the name of a class, how can I get the names of its slots?
  39.   [5-6]   Free CLOS software.
  40.  
  41. Search for \[#\] to get to question number # quickly.
  42.  
  43. In general, questions about object oriented programming in Lisp,
  44. especially questions about using CLOS or compiling PCL, should be
  45. directed to the newsgroup comp.lang.clos.
  46.  
  47. ----------------------------------------------------------------
  48. Subject: [5-0]  What is CLOS (PCL) and where can I get it?
  49.                 How do you pronounce CLOS?
  50.  
  51. CLOS (Common Lisp Object System) is the object-oriented programming
  52. standard for Common Lisp. It is the successor to Symbolics FLAVORS and
  53. Xerox LOOPS (Lisp Object Oriented Programming System). The acronym
  54. CLOS is pronouned either as "See-Loss" or "Closs", depending on taste.
  55. PCL (Portable Common Loops) is a portable CLOS implementation, and is
  56. available by anonymous ftp from 
  57.    parcftp.xerox.com:/pub/pcl/ [13.1.64.94]
  58. Also in the same directory are sources for CLX R5 and an inspecter.
  59.  
  60. Most Common Lisp implementations now include their own CLOS
  61. implementations. Common Lisp implementations with native CLOS include:
  62. MCL, {A}KCL, Allegro CL (including Allegro CL\PC), Ibuki, Lucid,
  63. Medley, Symbolics Genera, CLOE, and Harlequin Lispworks. CMU CL uses a
  64. customized version of PCL as their CLOS. However, not all native CLOS
  65. implementations have as detailed a meta-object protocol as PCL. For
  66. example, MCL 2.0 users sometimes use the july-1d version of PCL
  67. instead of the native CLOS for precisely this reason.
  68.  
  69. The book ``The Art of the Metaobject Protocol'' (see below) includes
  70. the CLOS Metaobject Protocol specification as chapters 5 and 6.  The
  71. sources for the MOP spec itself are available from 
  72.    parcftp.xerox.com:/pub/pcl/mop/ [13.1.64.94]
  73. as the file spec.tar.Z, but this is no substitute for buying the
  74. book, since the book contains a lot of useful explanatory material
  75. beyond the spec. The Closette files related to the book are also
  76. available from parcftp as the file closette.lisp.
  77.  
  78. The CLOS code repository is available by anonymous ftp to
  79.    nervous.cis.ohio-state.edu:/pub/lispusers/clos/ [128.146.61.200] 
  80. If you've got code you'd like to add to the repository, send mail to
  81. Arun Welch, commonloops-request@cis.ohio-state.edu. 
  82.  
  83. ----------------------------------------------------------------
  84. Subject: [5-1] What documentation is available about object-oriented
  85.                programming in Lisp? 
  86.  
  87. Books about object-oriented programming in Lisp include:
  88.  
  89.    1. dpANS CL describes the entire Common Lisp language, which includes the
  90.       CLOS standard.  Informally, CLtL2 can also be used to learn about CLOS, 
  91.       but please remember that CLtL2 is not an official X3J13 committee
  92.       document. (The presentation of CLtL2 differs from that of the draft
  93.       proposed standard, and some matters of fact have changed in the proposed
  94.       standard since the publication of CLtL2.)
  95.  
  96.    2. Sonya E. Keene
  97.       "Object-Oriented Programming in Common Lisp: 
  98.        A Programmer's Guide to CLOS"
  99.       Addison-Wesley (Reading, MA), 1989. 266 pages. ISBN 0-201-17589-4.
  100.            Tutorial introduction to CLOS with many examples and
  101.            a lot of good advice for designing large programs using CLOS.
  102.  
  103.    3. Jo A. Lawless and Molly M. Miller.
  104.       "Understanding CLOS: the Common Lisp Object System"
  105.       Digital Press, 1991. 192 pages.
  106.  
  107.    4. Gregor Kiczales, Jim des Rivieres, and Daniel G. Bobrow.
  108.       "The Art of the Metaobject Protocol"
  109.       MIT Press, 1991. 335 pages. ISBN 0-262-61074-4, $34.95.
  110.            The first part of the book presents a model CLOS implementation,
  111.            introduces the basic principles of metaobject protocols, and 
  112.            works through the key elements of the CLOS Metaobject Protocol.
  113.            The second half is the detailed specification of the CLOS
  114.            Metaobject Protocol. A simple working interpreter suitable
  115.            for experimentation is contained in an appendix.
  116.  
  117.    5. Robert R. Kessler and Amy R. Petajan.
  118.       "LISP, Objects, and Symbolic Programming"
  119.       Scott, Foresman and Company (Glenview, IL), 1988. 644 pages.
  120.            Includes a small Lisp compiler.
  121.  
  122.    6. A short introduction to CLOS written by Jeff Dalton of the
  123.       University of Edinburgh <J.Dalton@ed.ac.uk> is available by
  124.       anonymous ftp from  
  125.          aiai.ed.ac.uk:/lisp/random [192.41.104.6]
  126.       as the file clos-guide.
  127.  
  128.    7. Andreas Paepcke
  129.       "Object-Oriented Programming: the CLOS Perspective"
  130.       MIT Press, 1993, ISBN 0-262-16136-2.
  131.          This book is a collection of essays on the following topics:
  132.            -  Description of CLOS and its design philosophy.
  133.            -  The Metaobject Protocol and its use.
  134.            -  Comparison of CLOS with Smalltalk, Eiffel, Sather, and C++.
  135.            -  CLOS Uses and Methodology. Descriptions of two large CLOS
  136.               applications (Sun's LispView and a hybrid knowledge
  137.               representation tool) and an approach to documenting
  138.               object-oriented protocols (similar to that of AMOP).
  139.            -  Implementation details. Descriptions of TI CLOS for the
  140.               Explorer and PCL's method dispatch mechanism.
  141.  
  142. ----------------------------------------------------------------
  143. Subject: [5-2] How can I write a function that can access defstruct slots 
  144.                by name?  I would like to write something like 
  145.                (STRUCTURE-SLOT <object> '<slot-name>).
  146.  
  147. There is currently no portable, built-in way to access structure slots
  148. given only the name.  If your Common Lisp includes an implementation
  149. of CLOS that supports the meta-object protocol specified in the
  150. original X3J13 draft spec (document X3J13/88-003), then it probably will
  151. allow (SLOT-VALUE <object> '<slot-name>); however, not all
  152. implementations of CLOS currently provide this.  Lacking this, some
  153. implementations may provide implementation-dependent functions that
  154. allow access to structure slots by name; note that this may cause
  155. saved images to be larger, as some implementations normally open-code
  156. structure accessors and discard slot name information.
  157.  
  158. While it is not possible to write a fully general STRUCTURE-SLOT function,
  159. it is not very difficult to write version that handles specific structure
  160. types.  For instance, after defining:
  161.  
  162.    (defstruct spaceship name captain position velocity)
  163.  
  164. one may then define:
  165.  
  166.    (defun spaceship-slot (spaceship slot-name)
  167.      (ecase slot-name
  168.        (name (spaceship-name spaceship))
  169.        (captain (spaceship-captain spaceship))
  170.        (position (spaceship-position spaceship))
  171.        (velocity (spaceship-velocity spaceship))))
  172.  
  173. or using CLOS (generic functions):
  174.  
  175. (defgeneric spaceship-slot (spaceship slot-name)
  176.   (:method ((x spaceship) (slot (eql :name)))
  177.     (spaceship-name x))
  178.   (:method ((x spaceship) (slot (eql :captain)))
  179.     (spaceship-captain x))
  180.   (:method ((x spaceship) (slot (eql :position)))
  181.     (spaceship-position x))
  182.   (:method ((x spaceship) (slot (eql :velocity)))
  183.     (spaceship-velocity x)))
  184.  
  185. Another popular way to define this is:
  186.  
  187.    (defun spaceship-slot (spaceship slot-name)
  188.      (funcall (symbol-function
  189.                 (find-symbol (format nil "SPACESHIP-~A" slot-name)
  190.                              #.(package-name *package*)))
  191.               spaceship))
  192.  
  193. I personally recommend the first version.  It is likely to be much faster
  194. and more memory efficient than the second version.  It's also easy to get
  195. the second one wrong; many people forget to specify the package argument to
  196. FIND-SYMBOL, which can cause incorrect results when the package at run time
  197. is different from the one at compile time.  Even my version assumes that
  198. SPACESHIP-SLOT is being defined in a file that is in the same package as
  199. the one containing the structure definition; if this isn't the case,
  200. #.(PACKAGE-NAME *PACKAGE*) should be replaced by a string naming the
  201. correct package.
  202.  
  203. Another workaround is to define a MY-DEFSTRUCT macro that parses the
  204. defstruct arguments and expands into a call to DEFSTRUCT along with a
  205. definition of the runtime slot-accessor function.
  206.  
  207. Some non-portable techniques include the use of SYSTEM:STRUCTURE-REF
  208. in Lucid (LCL:STRUCTURE-REF in earlier versions of Lucid) and 
  209. EXCL:STRUCTURE-REF in Allegro. 
  210. ----------------------------------------------------------------
  211. Subject: [5-3] How can I list all the CLOS instances in a class?
  212.  
  213. There is no built-in way to enumerate the instances of a class.  If you are
  214. only interested in listing the instances of classes that you have defined,
  215. it is not very difficult to implement it as part of your class definition.
  216. Add a shared slot, e.g. ALL-INSTANCES, with an initial value of NIL, to the
  217. class definition.  Then write an after-method on INITIALIZE-INSTANCE for
  218. this class, which pushes the instance being initialized onto ALL-INSTANCES.
  219. Note that this must be done separately for each class that wants to maintain
  220. such a list; it can't be encapsulated in a mixin class, because all its
  221. dependent classes would share the same ALL-INSTANCES slot.  A compromise
  222. would be to use a mixin to define the INITIALIZE-INSTANCE after-method (and
  223. any other general-purpose methods that use the slot), but not the shared
  224. slot; it would be up to the descendant classes to define the slot at the
  225. level of the class hierarchy that is appropriate. You could also try
  226. defining the classes that need instance-recording as instances of a
  227. metaclass that holds the instance registry on the class object. The
  228. recording behavior could then be built-in to an after method on
  229. initialize-instance for the root class of the metaclass, or even
  230. allocate-instance. To allow for garbage collection of old instances,
  231. you will also need to define a generic function to remove the recorded
  232. instances from the list of instances.
  233. ----------------------------------------------------------------
  234. Subject: [5-4]  How can I store data and CLOS instances (with possibly 
  235.                 circular references) on disk so that they may be
  236.                 retrieved at some later time?
  237.  
  238. There are two main techniques of doing this kind of persistent object
  239. storage. The first involves using #. to compile the data into a file.
  240. The second produces an ASCII representation which, when evaluated,
  241. will reproduce an equivalent set of data.
  242.  
  243. If the data you wish to save is stored in the variable *hash-table*,
  244. create a file containing just the lines
  245.         (in-package "YOUR-PACKAGE")
  246.         (setq *hash-table* '#.*hash-table*)
  247. and compile it. The #. macro performs read-time evaluation of the
  248. expression following the dot, and so this compiles the data into the
  249. file. You may then load the file to restore the data. However, the
  250. resulting binary file is not portable between Lisp implementations,
  251. and sometimes not even for the same Lisp on different platforms. Also,
  252. some Lisps will treat the data as constant, and place it on pages in
  253. memory that are marked read-only (after it is loaded). If one tries to
  254. later modify the data, these Lisps will signal an error. Lucid CL only
  255. puts such constants in a read-only area when they appear inside
  256. functions, so this should be safe. Allegro CL doesn't seem to complain
  257. about modification if the data is a cons. DEC's VAXLisp, however, has
  258. problems with #. circular structures in .fas files. MCL seems to work
  259. well with using #. to save data (and even functions) to a file.
  260.  
  261. The other technique is to produce an ASCII representation of the Lisp
  262. objects which may then be saved to a file. To reproduce the data, one
  263. can load (or compile and load) the file. This technique is portable
  264. between different Lisps and platforms. Unfortunately, the resulting
  265. data is not necessarily EQ to the original. Kerry Koitzsch's
  266. save-object.lisp package is included in the Lisp Utilities Repository,
  267. ftp.cs.cmu.edu:/user/ai/lang/lisp/code/ext/save_obj/.
  268. The Lisp Utilities Repository is described in detail in the answer to
  269. question [6-1]. 
  270.  
  271. See also the discussion of MAKE-LOAD-FORM and MAKE-LOAD-FORM-SAVING-SLOTS 
  272. in CLtL2.
  273.  
  274. WOOD (William's Object Oriented Database) is a simple persistent object
  275. store for MCL 2.0, written by Bill St. Clair <bill@cambridge.apple.com>.
  276. Its goal is to provide a way to save/restore Lisp objects to/from disk.
  277. It is available by anonymous ftp from the MCL repository on
  278. cambridge.apple.com:/pub/MCL2/CONTRIB/. Send bug reports to
  279. bug-wood@cambridge.apple.com. To be added to the mailing list, send mail
  280. to info-wood-request@cambridge.apple.com.
  281.  
  282. Statice is a commercial product from Symbolics, that provides a
  283. powerful persistent ODBMS. It runs on Symbolics Lisp Machines and soon
  284. on Unix boxes under Lucid. 
  285.  
  286. ITASCA ODBMS V2.2 is a distributed active object database management
  287. system. ITASCA allows clients to transparently access data that is
  288. distributed among multiple servers.  ITASCA supports full dynamic
  289. schema modification that can be performed during any phase of the
  290. software lifecycle.  Applications written in dissimilar and
  291. incompatible languages, such as C++ and CLOS, share objects through
  292. ITASCA. ITASCA stores methods inside the database, promoting
  293. reusability and maintainability.  ITASCA is based on work at MCC's
  294. Object-Oriented and Distributed Systems Lab on the ORION system. For
  295. more information, write to Itasca Systems, Inc., 7850 Metro Parkway,
  296. Minneapolis, MN 55425, sales@itasca.com, 612-851-3155, fax 612-851-3157.
  297.  
  298. ----------------------------------------------------------------
  299. Subject: [5-5] Given the name of a class, how can I get the names
  300.                of its slots?     
  301.  
  302. (defun class-slot-names (class-name)
  303.   "Given a CLASS-NAME, returns a list of the slots in the class."
  304.   (mapcar #'clos:slot-definition-name
  305.           (clos:class-slots (find-class class-name))))
  306.  
  307. (defmethod class-slot-names ((instance standard-object))
  308.   "Given an INSTANCE, returns a list of the slots in the instance's class."
  309.   (mapcar #'clos:slot-definition-name
  310.           (clos:class-slots (class-of instance))))
  311.  
  312. You can use CLASS-DIRECT-SLOTS instead of CLASS-SLOTS if you don't
  313. want inherited slots. Note that these functions are from the
  314. meta-object protocol specified in the original X3J13 draft spec
  315. (document X3J13/88-003), and may not be supported by all Lisps.
  316.  
  317. ----------------------------------------------------------------
  318. Subject: [5-6] Free CLOS software.
  319.  
  320. Software Repositories:
  321.  
  322.    The CLOS code repository is available by anonymous ftp to
  323.    nervous.cis.ohio-state.edu:/pub/lispusers/clos/ [128.146.61.200]
  324.    If you've got code you'd like to add to the repository, send
  325.    mail to Arun Welch, commonloops-request@cis.ohio-state.edu.
  326.    The CLOS code repository includes dag.lisp.Z and 3DGeometry.lisp.
  327.  
  328. CORBA:
  329.  
  330.    The CORBA (Common Object Request Broker Architecture) specification's
  331.    Dynamic Invocation Interfaces (DII) are a collection of interfaces for
  332.    writing extensible, distributed object applications.  NEC Systems
  333.    Laboratory Inc. has contributed both C and CLOS implementations of
  334.    these specification to the Object Management Group (OMG). The CORBA
  335.    specification (91-8-1) is available free to OMG members, and for $50
  336.    to non-members. To order the document, send mail to documents@omg.org,
  337.    or write to OMG, 492 Old Connecticut Path, Framingham, MA 01701, phone
  338.    508-820-4300, fax 508-820-4303. The DII implementation can be
  339.    retrieved via anonymous ftp from omg.org:/pub/ [192.67.184.64] as the
  340.    file NEC_DII/93-1-2.tar.Z. There are three dependencies in the
  341.    contributed DII implementations.  First, the CLOS images require an
  342.    Allegro CL license due to its use of the Lisp foreign function
  343.    interface.  Second, the C executables require Sun SparcStations, but
  344.    they can be re-compiled and re-linked for other architectures.  Third,
  345.    the implementation of the `Invoke' DII interface is considered
  346.    proprietary.  For further information, write to Don Vines, NEC Systems
  347.    Laboratory, Inc., C&C Software Technology Center (CSTC), 1901 Gateway
  348.    Drive, Irving, Texas 75038, call 214-518-3486, fax 214-518-3552, or
  349.    send email to dvines@syl.dl.nec.com.
  350.  
  351. ----------------------------------------------------------------
  352. ;;; *EOF*
  353.  
  354.  
  355.